package edu.yuhf.streamdemo;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class BuildStream {

    //生成随机数
    public void randomNumber(){
        Stream.generate(()->Math.random()).limit(5).forEach(System.out::println);
    }
    //生成无限流操作
    public void generateOperation(){
        Stream.iterate(0, i -> i + 1).limit(5).forEach(i -> System.out.print(i + " "));
    }
    //将数组转换成流并进行操作
    public void arrayOperation(){
        Integer[] arrays = {1,32,4,5353,34,34,34,36,6,454,2};
        Stream<Integer> stream = Stream.of(arrays);
        stream.filter(element->element>30).forEach(System.out::println);
                //.forEach(System.out::println);
        //Arrays.stream(arrays).filter(element->element>30).forEach(System.out::println);
    }
    //直接将值转变为流并进行操作
    public void ofOperation(){
        Stream<String> stream = Stream.of("git","github.com","gitee.com","java","javascript","python");
        stream.skip(1).filter(new Predicate<String>() {
            public boolean test(String value) {
                return value.indexOf(".")==-1;
            }
        }).map(new Function<String, String>() {
            public String apply(String value) {
                return value.toUpperCase();
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String value) {
                System.out.println(value);
            }
        });
    }
    //将集合转化为流并进行操作
    public void streamOperation(){
        String[] values = {"ok","hello","test","javascript","java"};
        var list = new ArrayList<>(List.of(values));
        List<String> list0 = list.stream().filter(value->value.startsWith("j")).collect(Collectors.toList());
        //list.stream().filter(value->value.startsWith("j")).forEach(System.out::println);
        //List<String> list0 = list.stream().filter(value->value.startsWith("j")).collect(Collectors.toList());
        list0.stream().forEach(element->System.out.println(element));
        list0.stream().forEach(System.out::println);
    }

    public void ofOperation1() {
        new BuildStream().ofOperation();
        //获取10个随机数，并存入集合中
        List<Integer> list = Stream.generate(Math::random).limit(10).map(value->{
            var stringValue = value.toString();
            return Integer.valueOf(stringValue.substring(2,4));
        }).collect(Collectors.toList());
        System.out.println("生成了"+list.size()+"个两位的随机数：具体数字如下：");
    }

    public void arrayInit(){
        int[] arrays = new int[10];
        IntStream.range(0,10).forEach(value->arrays[value]=value);
        System.out.println(Arrays.toString(arrays));
        IntStream.range(0,10).map(value->value*10).forEach(System.out::println);
        OptionalInt optional = IntStream.rangeClosed(0,100).reduce(Integer::sum);
        optional.ifPresent(System.out::println);
    }

    static class MyConsumer implements Consumer<String> {
        @Override
        public void accept(String element) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        Stream<String> stream = Stream.of("git","github.com","gitee.com","java","javascript","python");
        //利用collect()方法将stream流和另一个流合并
        var list = stream.collect(Collectors.toList());
    }
}
